package com.base.properties
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * 配置文件
	 * @author Administrator
	 */	
	public class Properties extends EventDispatcher {
		
		private var _properties:Object;
		
		private var _loader:URLLoader;
		
		public function Properties( url:String ) {
			super(this);
			_properties = {};
			_loader = new URLLoader();
			_loader.addEventListener(Event.COMPLETE, onLoaderComplete);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			_loader.load( new URLRequest(url) );
		}
		
		public function getProperty(key:String):String {
			return _properties[key];
		}
		
		public function setProperty(key:String, value:String):void {
			_properties[key] = value;
		}
		
		public function get propertyNames():Array {
			var result:Array = [];
			for (var key:String in _properties) {
				result.push(key);
			}
			return result;
		}
		
		private function onLoaderComplete(event:Event):void {
			var lines:Array = ( _loader.data as String).split( "\r\n" );
			
			for each ( var line:String in lines ) {
				var pair:Array = line.split( "=" );
				setProperty( pair[0] , pair[1] );
			}
		}
		
		private function onIOError(event:IOErrorEvent):void {
			dispatchEvent(event);
		}
		
	}
}